﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MulagoDb.database;
using MulagoDb.model;
using System.Collections;

namespace MulagoDb
{
    public partial class Hospitalization : UserControl
    {
        private Patient patient;
        public Hospitalization()
        {
            InitializeComponent();
        }

        private void Hospitalization_Load(object sender, EventArgs e)
        {
            patientLbl.Text = patientLbl.Text + " " + FindPatient.patienceName;
            patient = PatientReg.biodata;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void treeView8_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView6_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView5_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView7_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView11_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView4_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView10_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView3_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView9_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBox12_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void richTextBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void groupBox6_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label21_Click(object sender, EventArgs e)
        {

        }

        private void label35_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label19_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label13_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void label38_Click(object sender, EventArgs e)
        {

        }

        private void label28_Click(object sender, EventArgs e)
        {

        }

        private void checkBox15_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label31_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string location = "";
            if (radioButton1.Checked)
            {
                location = radioButton1.Text;
            }
            else if(radioButton2.Checked){
                location = otherHospitalCombo.Text;
            }
            HospitalizationMajor hm = new HospitalizationMajor();
            hm.patient = patient;
            hm.location = location;
            hm.admissiondate = DateTime.Parse(admissionDatePicker.Text);
            hm.dischargedate = DateTime.Parse(dischargeDatetimePicker.Text);
            hm.numberofDays = Convert.ToInt32(numDaysinHospTxtFld.Text);
            hm.admissionage = monthRadio.Checked ?Convert.ToInt32(monthCombo.Text) : Convert.ToInt32(yearsCombo.Text);
            hm.history = historyTfld.Text;
            HospitalizationDB.inserthospitalization_major(hm);

            HospitalizationPhysicalexamination hpe = new HospitalizationPhysicalexamination();
            hpe.patient = patient;
            hpe.pallor = pallorCombo.Text;
            hpe.juandice = juandiceCombo.Text;
            hpe.liver =  liverCombo.Text;
            hpe.spleen = spleenCombo.Text;
            hpe.other = otherTfld.Text;
            HospitalizationDB.inserthospitalization_physicalexamination(hpe);

            HospitalizationInvestigations hi = new HospitalizationInvestigations();
            hi.patient = patient;
            hi.hb = hbTfld.Text;
            hi.mcv = mcvTfld.Text;
            hi.wbc = wbcTfld.Text;
            hi.neutrophils = neutrophilsTfld.Text;
            hi.platelets = platelatesTfld.Text;
            hi.malaria =  malariaCombo.Text;
            hi.reticulocytes = reticulocytesTfld.Text;
            hi.bilirubinDirect = bilirubinDirectTfl.Text;
            hi.bilirubinTotal = bilirubintotalTfld.Text;
            hi.other = other1Tfld.Text;
            HospitalizationDB.inserthospitalization_investigations(hi);

            MessageBox.Show(this, "Successfully added"+location);
        }

        private void textBox17_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox20_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label36_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void label27_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void treeView12_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void checkBox19_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void grid1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label24_Click(object sender, EventArgs e)
        {

        }

        private void grid2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox17_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox14_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label29_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBox10_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label32_Click(object sender, EventArgs e)
        {

        }

        private void comboBox9_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label37_Click(object sender, EventArgs e)
        {

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label34_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox18_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label25_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void checkBox21_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox12_CheckedChanged_1(object sender, EventArgs e)
        {

        }

        private void comboBox11_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void AcutechestsyndromecheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void label18_Click(object sender, EventArgs e)
        {

        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox16_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void label23_Click(object sender, EventArgs e)
        {

        }

        private void monthRadio_CheckedChanged(object sender, EventArgs e)
        {
            yearsCombo.Enabled = false;
            monthCombo.Enabled = true;
        }

        private void yearsRadio_CheckedChanged(object sender, EventArgs e)
        {
            monthCombo.Enabled = false;
            yearsCombo.Enabled = true;
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
        public ArrayList getTreeNodeState(TreeView tree)
        {
            ArrayList nodeStates = new ArrayList();
            foreach (TreeNode pnode in tree.Nodes)
            {
                foreach (TreeNode cnode in pnode.Nodes)
                {
                    if (cnode.Checked)
                    {
                        nodeStates.Add("T");
                    }
                    else
                    {
                        nodeStates.Add("F");
                    }
                }
            }
            return nodeStates;
        }
        public ArrayList getTreatment()
        {
            ArrayList nodestates = new ArrayList();
            string observation = "";
            string analgesia = "";
            string surgery = "";
            string antibiotics = "";
            string rbctransfusion = "";
            string antimalarials = "";
            string delivery = "";
            string other = "";
            foreach (TreeNode cnode in treatmentTreeView.Nodes)
            {
                    //go deep in each node to get child states
                    if (cnode.Text.Equals("Observation Only".Trim()))
                    {
                        observation = cnode.Checked ? "T" : "F";
                    }
                    else if (cnode.Text.Trim().Equals("Analgesia".Trim()))
                    {
                        analgesia = cnode.Checked ? "T" : "F";
                    }
                    else if (cnode.Text.Trim().Equals("Antibiotics".Trim()))
                    {
                        antibiotics = getState(cnode).ToString();
                    }
                    else if (cnode.Text.Trim().Equals("Antimalarials".Trim()))
                    {
                        antimalarials = getState(cnode).ToString();
                    }
                    else if (cnode.Text.Trim().Equals("RBC transfusion".Trim()))
                    {
                        rbctransfusion = getState(cnode).ToString();
                    }
                    else if (cnode.Text.Trim().Equals("Surgery".Trim()))
                    {
                        surgery = getState(cnode).ToString();
                    }
                    else if (cnode.Text.Trim().Equals("Delivery".Trim()))
                    {
                        delivery = getState(cnode).ToString();
                    }
                    else if (cnode.Text.Trim().Equals("Other – specify".Trim()))
                    {
                        other = getState(cnode).ToString();
                    }
            }
            nodestates.Add(observation);
            nodestates.Add(analgesia);
            nodestates.Add(antibiotics);
            nodestates.Add(antimalarials);
            nodestates.Add(rbctransfusion);
            nodestates.Add(surgery);
            nodestates.Add(delivery);
            nodestates.Add(other);
            return nodestates;
        
        }
        public StringBuilder getState(TreeNode parent)
        {
            StringBuilder nodestates = new StringBuilder();
            foreach (TreeNode tnode in parent.Nodes)
            {
                nodestates.Append(tnode.Checked ? "T," : "F,");
            }
            return nodestates;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            HospitalizationSepsis hs = new HospitalizationSepsis();
            ArrayList sepsis = getTreeNodeState(sepsisTreeview);
            hs.patient = patient;
            hs.pneumonia = Convert.ToChar(sepsis[0].ToString());
            hs.influenza = Convert.ToChar(sepsis[1].ToString());
            hs.salmonella =  Convert.ToChar(sepsis[2].ToString());
            hs.stapaureus =  Convert.ToChar(sepsis[3].ToString());
            hs.other = Convert.ToChar(sepsis[4].ToString());
            hs.unknown = Convert.ToChar(sepsis[5].ToString());
            HospitalizationDB.inserthospitalization_sepsis(hs);

            //2
            ArrayList menengitis = getTreeNodeState(menengitisTreeview);
            HospitalizationMenengitis hmen = new HospitalizationMenengitis();
            hmen.patient = patient;
            hmen.pneumonia = Convert.ToChar(menengitis[0].ToString());
            hmen.influenza = Convert.ToChar(menengitis[1].ToString());
            hmen.other = Convert.ToChar(menengitis[2].ToString());
            hmen.unknown =  Convert.ToChar(menengitis[3].ToString());
            HospitalizationDB.inserthospitalization_menengitis(hmen);

            //3
            ArrayList osteoyelitis = getTreeNodeState(osteomyelitisTreeview);
            HospitalizationOsteomyelitis hos = new HospitalizationOsteomyelitis();
            hos.patient = patient;
            hos.location = Convert.ToChar(osteoyelitis[0].ToString());
            hos.cause = osteoyelitis[1].ToString();
            HospitalizationDB.inserthospitalization_osteomyelitis(hos);

            //4
            ArrayList severeanaemia = getTreeNodeState(severeanaemiaTreeview);
            HospitalizationSeveranaemia hosan = new HospitalizationSeveranaemia();
            hosan.patient = patient;
            hosan.malaria = Convert.ToChar(severeanaemia[0].ToString());
            hosan.acuteaplasia = Convert.ToChar(severeanaemia[1].ToString());
            hosan.hypersplenism = Convert.ToChar(severeanaemia[2].ToString());
            hosan.hyperhemolysis = Convert.ToChar(severeanaemia[3].ToString());
            hosan.hyperhemolysisNotPosttransfusion = Convert.ToChar(severeanaemia[4].ToString());
            hosan.ironDeficiencyAnemia = Convert.ToChar(severeanaemia[5].ToString());
            hosan.other = Convert.ToChar(severeanaemia[6].ToString());
            HospitalizationDB.inserthospitalization_severanaemia(hosan);

            //5
            ArrayList dactylitis = getTreeNodeState(dactylitisTreeview);
            HospitalizationDactylitis hosda = new HospitalizationDactylitis();
            hosda.patient = patient;
            hosda.righthand = Convert.ToChar(dactylitis[0].ToString());
            hosda.lefthand = Convert.ToChar(dactylitis[1].ToString());
            hosda.rightfoot = Convert.ToChar(dactylitis[2].ToString());
            hosda.leftfoot = Convert.ToChar(dactylitis[3].ToString());
            HospitalizationDB.inserthospitalization_dactylitis(hosda);

            //6
            ArrayList acutepain = getTreeNodeState(acutepainTreeview);
            HospitalizationAcutepain hacu = new HospitalizationAcutepain();
            hacu.patient = patient;
            hacu.thorax = Convert.ToChar(acutepain[0].ToString());
            hacu.abdomen = Convert.ToChar(acutepain[1].ToString()); 
            hacu.back = Convert.ToChar(acutepain[2].ToString());
            hacu.hips = Convert.ToChar(acutepain[3].ToString());
            hacu.lupperLimb = Convert.ToChar(acutepain[4].ToString());
            hacu.llowerLimb = Convert.ToChar(acutepain[5].ToString());
            hacu.rupperLimb = Convert.ToChar(acutepain[6].ToString());
            hacu.rlowerLimb = Convert.ToChar(acutepain[7].ToString());
            hacu.other = acutepain[8].ToString();
            HospitalizationDB.inserthospitalization_acutepain(hacu);

            //7
            ArrayList abnormaltcd = getTreeNodeState(abnormaltcdTreeview);
            HospitalizationAbnormaltcd hab = new HospitalizationAbnormaltcd();
            hab.patient = patient;
            hab.rica = Convert.ToChar(abnormaltcd[0].ToString());
            hab.rmca = Convert.ToChar(abnormaltcd[1].ToString());
            hab.lica = Convert.ToChar(abnormaltcd[2].ToString());
            hab.lmca = Convert.ToChar(abnormaltcd[3].ToString());
            hab.other = Convert.ToChar(abnormaltcd[4].ToString());
            HospitalizationDB.inserthospitalization_abnormaltcd(hab);

            //8
            ArrayList stroke = getTreeNodeState(strokeTreeview);
            HospitalizationStroke hostr = new HospitalizationStroke();
            hostr.patient = patient;
            hostr.rupperLimb = Convert.ToChar(stroke[0].ToString());
            hostr.rlowerLimb = Convert.ToChar(stroke[1].ToString());
            hostr.lupperLimb = Convert.ToChar(stroke[2].ToString());
            hostr.llowerLimb = Convert.ToChar(stroke[3].ToString());
            hostr.dysphasia = Convert.ToChar(stroke[4].ToString());
            hostr.cranialnerve = Convert.ToChar(stroke[5].ToString());
            hostr.other = Convert.ToChar(stroke[6].ToString());
            HospitalizationDB.inserthospitalization_stroke(hostr);

            //9
            ArrayList cholecystitis = getTreeNodeState(cholecystitisTreeview);
            HospitalizationCholecystitis hoch = new HospitalizationCholecystitis();
            hoch.patient = patient;
            hoch.acute = Convert.ToChar(cholecystitis[0].ToString());
            hoch.subacute = Convert.ToChar(cholecystitis[1].ToString());
            HospitalizationDB.inserthospitalization_cholecystitis(hoch);

            //10
            ArrayList avascular = getTreeNodeState(acascularnecrosisTreeview);
            HospitalizationAvascularnecrosis hoava = new HospitalizationAvascularnecrosis();
            hoava.patient = patient;
            hoava.rhumerus = Convert.ToChar(avascular[0].ToString());
            hoava.lhumerus = Convert.ToChar(avascular[1].ToString());
            hoava.rfemarolhead = Convert.ToChar(avascular[2].ToString());
            hoava.lfemoralhead = Convert.ToChar(avascular[3].ToString());
            hoava.other = Convert.ToChar(avascular[4].ToString());
            HospitalizationDB.inserthospitalization_avascularnecrosis(hoava);

            //11
            ArrayList legulcer = getTreeNodeState(legulcerTreeview);
            HospitalizationLegulcer hosleg = new HospitalizationLegulcer();
            hosleg.patient = patient;
            hosleg.rightankle = Convert.ToChar(legulcer[0].ToString());
            hosleg.leftankle = Convert.ToChar(legulcer[1].ToString());
            hosleg.other = Convert.ToChar(legulcer[2].ToString());
            HospitalizationDB.inserthospitalization_legulcer(hosleg);

            //insert general
            HospitalizationDiagnosisgeneral hdgeneral = new HospitalizationDiagnosisgeneral();
            hdgeneral.patient = patient;
            hdgeneral.acutechest = Convert.ToChar(AcutechestsyndromecheckBox.Checked ? "T" : "F");
            hdgeneral.probableMalaria = Convert.ToChar(probablemalariaCheckbox.Checked ? "T" : "F");
            hdgeneral.tbSuspected = Convert.ToChar(tbsuspectedCheck.Checked ? "T" : "F");
            hdgeneral.otherRespiratory = Convert.ToChar(otherrespitoryCheck.Checked ? "T" : "F");
            hdgeneral.gastroenteritis = Convert.ToChar(gastroenteritisCheck.Checked ? "T" : "F");
            hdgeneral.probableuti = Convert.ToChar(probableUtiCheck.Checked ? "T" : "F");
            hdgeneral.febrileillness = Convert.ToChar(febrileillnessCheck.Checked ? "T" : "F");
            hdgeneral.acutesplenicsequestration = Convert.ToChar(acutesplenicCheck.Checked ? "T" : "F");
            hdgeneral.subacute = Convert.ToChar(subacuteCheck.Checked ? "T" : "F");
            hdgeneral.asthma = Convert.ToChar(asthmaCheck.Checked ? "T" : "F");
            hdgeneral.chronichypoxemia = Convert.ToChar(chronichypoxemiaCheck.Checked ? "T" : "F");
            hdgeneral.pulmonaryhypertension = Convert.ToChar(pulmonaryhypertensionCheck.Checked ? "T" : "F");
            hdgeneral.headache = Convert.ToChar(headacheCheck.Checked ? "T" : "F");
            hdgeneral.chronicrenalfailure = Convert.ToChar(chronicrenalfailureCheck.Checked ? "T" : "F");
            hdgeneral.priapism = Convert.ToChar(priapismCheck.Checked ? "T" : "F");
            hdgeneral.multiorganfailure =  Convert.ToChar(multorganCheck.Checked ? "T" : "F");
            hdgeneral.pregnance = Convert.ToChar(preganceCheck.Checked ? "T" : "F");
            hdgeneral.other = Convert.ToChar(otherCheck.Checked ? "T" : "F");
            HospitalizationDB.inserthospitalization_diagnosisgeneral(hdgeneral);
            MessageBox.Show(this, "Successfully submitted data");
        }

        private void submitBtn_Click(object sender, EventArgs e)
        {
            ArrayList treatment = getTreatment();
            HospitalizationDB.inserthospitalization_treatment(FindPatient.patientId, treatment[0].ToString(),
                treatment[1].ToString(),treatment[2].ToString(),treatment[3].ToString(), treatment[4].ToString(),
                treatment[5].ToString(), treatment[6].ToString(), treatment[7].ToString());
            MessageBox.Show(this, "Successfully submitted data");
        }
    }
}
